package org.keyroy.mapEditor.ui.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class TextInputDialog extends Shell {
	private Text text;

	/**
	 * Create the shell.
	 * 
	 * @param display
	 */
	public TextInputDialog(Shell Shell, final OnClickListener clickListener) {
		super(Shell.getDisplay(), SWT.DIALOG_TRIM);
		setImage(SWTResourceManager.getImage(TextInputDialog.class, "/res/icons/write_obj.gif"));
		setLayout(new FormLayout());

		text = new Text(this, SWT.BORDER);
		FormData fd_text = new FormData();
		fd_text.right = new FormAttachment(100, -10);
		fd_text.top = new FormAttachment(0, 10);
		fd_text.left = new FormAttachment(0, 10);
		text.setLayoutData(fd_text);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				clickListener.onClick(text.getText());
				dispose();
			}
		});
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.top = new FormAttachment(text, 6);
		fd_btnNewButton.right = new FormAttachment(100, -10);
		fd_btnNewButton.left = new FormAttachment(100, -124);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("\u786E\u8BA4");
		createContents();
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("SWT Application");
		setSize(331, 98);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public static interface OnClickListener {
		public void onClick(String text);
	}

}
